package com.itheima.case2.service.impl;

import com.itheima.case2.dao.UserLoginMapper;
import com.itheima.case2.pojo.po.UserLogin;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserLoginServiceImpl {
    /*
     * 用户登录功能
     * */
    public UserLogin login(UserLogin user) {
        //调用工具类获取会话对象
        SqlSession sqlSession = SqlSessionUtil.getSession(false);
        //获取接口代理类
        UserLoginMapper mapper = sqlSession.getMapper(UserLoginMapper.class);
        //使用代理类调用方法
        UserLogin loginUser = mapper.login(user);
        //释放资源
        sqlSession.close();
        return loginUser;
    }

    /*
     * 用户注册对象
     * */
    public boolean register(UserLogin user) {
        SqlSession sqlSession = null;
        try {
            //调用工具类获取会话对象  需要提交事务
            sqlSession = SqlSessionUtil.getSession();
            //获取接口代理类
            UserLoginMapper mapper = sqlSession.getMapper(UserLoginMapper.class);
            //调用根据用户名查询用户是否存在
            UserLogin userByName = mapper.queryUserByName(user.getUsername());
            //如果用户不存在就可以注册
            if(userByName==null){
                mapper.insertUser(user);
                return true;
            }else {
                //注册失败
                return false;
            }
        } finally {
            sqlSession.close();
        }
    }
}
